package com.project.service.impl;

import com.project.dao.UserDOMapper;
import com.project.dao.VipUserDOMapper;
import com.project.dataobject.UserDO;
import com.project.dataobject.VipUserDO;
import com.project.service.MailService;
import com.project.service.UserService;
import com.project.service.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Objects;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDOMapper userDOMapper;

    @Autowired
    private VipUserDOMapper vipUserDOMapper;

    /**
     * 预警阈值
     */
    private Integer THRESHOLD1 = 10000;

    private Integer THRESHOLD2 = 5000;

    private Integer THRESHOLD3 = 1000;

    private Integer THRESHOLD4 = 0;

    @Autowired
    private MailService mailService;


    @Override
    public Boolean userRegister(String telphone, String password,String mailAddress) {
        UserDO userDO = new UserDO();
        userDO.setUserTelphone(telphone);
//        userDO.setUserPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userDO.setUserPassword(password);
        userDO.setUserMailaddress(mailAddress);
        // 写入数据库
        try{
            userDOMapper.insertSelective(userDO);
        }catch (DuplicateKeyException e){
            // 唯一索引冲突
            return false;
        }
        return true;
    }

    @Override
    public UserModel userLogin(String telphone, String password) {
        // 密码加密
//        String encodedPassword = DigestUtils.md5DigestAsHex(password.getBytes());
        // 比较密码是否相等
        UserDO userDO = userDOMapper.selectByTelphone(telphone);
        if (userDO == null){
            return null;
        }
        if (!userDO.getUserPassword().equals(password)){
            return null;
        }
        // 生成token
        UserModel userModel = new UserModel();
        String token = UUID.randomUUID().toString().replace("-","");
        // 更新token存储
        userDOMapper.updateToken(userDO.getUserId(),token);
        userModel = this.convertFromUserDO(userDO);
        userModel.setToken(token);
        // 类型转换
        return userModel;
    }

    private UserModel convertFromUserDO(UserDO userDO){
        if (userDO == null){
            return null;
        }
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(userDO,userModel);
        return userModel;
    }

    @Override
    public void prePaid(Integer userId, Integer total) {

        // 写入数据库
        VipUserDO vipUserDO = new VipUserDO();
        vipUserDO.setUserId(userId);
        vipUserDO.setTotal(total);
        try{
            vipUserDOMapper.insertSelective(vipUserDO);
        }catch (DuplicateKeyException exception){
            throw exception;
        }
    }

    @Override
    public Integer getServiceRemain(Integer userId) {
        VipUserDO vipUserDO = vipUserDOMapper.selectByUserId(userId);
        // 剩余次数达到预警阈值
        Integer total = vipUserDO.getTotal();
        // 发送邮件
        if (Objects.equals(total, THRESHOLD1) || Objects.equals(total, THRESHOLD2) || Objects.equals(total, THRESHOLD3) || Objects.equals(total, THRESHOLD4)){
            // 查询邮箱地址
            UserDO userDO = userDOMapper.selectByPrimaryKey(userId);
            String mailAddress = userDO.getUserMailaddress();
            // 调用接口发送邮件
            mailService.sendUserWarning(total,mailAddress);
        }
        // 返回剩余服务次数
        return total;
    }

    @Override
    public void decreaseServiceRemain(Integer userId, Integer count) {
        vipUserDOMapper.updateRemainCount(userId,count);
    }
}
